/*
 * @Author: LIFM0623 1944099171@qq.com
 * @Date: 2024-01-12 21:45:09
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-05-15 21:19:32
 * @FilePath: \Scene_Back\src\marker\marker.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Req,
  Res,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { MarkerService } from './marker.service';
import { CreateMarkerDto } from './dto/create-marker.dto';
import { updateMarker } from './dto/update-marker.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import type { Response } from 'express';
import { zip } from 'compressing';
import { Request } from 'express';
import * as fs from 'fs';
import * as path from 'path';
@Controller('marker')
export class MarkerController {
  constructor(private readonly markerService: MarkerService) {}

  @Post('uploadMarker')
  @UseInterceptors(FileInterceptor('file'))
  uploadMarker(@UploadedFile() file) {
    return file.filename;
  }

  @Post('createMarkerItem')
  createMarkerItem(
    @Body() createMarkerDto: CreateMarkerDto,
    @Req() request: Request,
  ) {
    const user = request.user;
    createMarkerDto.creator = (user as any).email;
    return this.markerService.createMarkerItem(createMarkerDto);
  }

  @Get('downLoadSingleMarkerPicture')
  downLoadSingleMarkerPicture(@Res() res: Response) {
    const url = process.env.FILE_PATH + '/marker/1705306887596.jpg';
    res.download(url);
  }

  @Get('downLoadSingleMarkerPictureStream')
  async downLoadSingleMarkerPictureStream(@Res() res: Response) {
    const url = process.env.FILE_PATH + '/marker/1705306887596.jpg';
    const tarStream = new zip.Stream();
    await tarStream.addEntry(url);

    res.setHeader('Content-Type', 'application/octet-stream');

    res.setHeader('Content-Disposition', `attachment; filename=xiaoman`);

    tarStream.pipe(res);
  }

  @Get('getMarkerPictureByName/:markerName')
  async getMarkerPic(
    @Param('markerName') markerName: string,
    @Res() res: Response,
  ) {
    // 构建本地文件路径
    const filePath = path.resolve(
      process.env.FILE_PATH + '/marker/' + markerName,
    );

    // 创建可读流
    const fileStream = fs.createReadStream(filePath);
    res.setHeader('Content-Type', 'image/png');
    // 设置响应头，告诉浏览器返回的是文件
    // 将文件流传输给客户端
    fileStream.pipe(res);
  }

  @Get('getAllMarkerItem')
  getAllMarker() {
    return this.markerService.findAll();
  }

  @Get('getMarkerItemByUser/:creator')
  getMarkerItemByUser(@Param('creator') creator: string) {
    return this.markerService.getMarkerItemById(creator);
  }

  @Get('getMarkerItemById/:id')
  getMarkerItemById(@Param('id') id: string) {
    return this.markerService.getMarkerItemById(id);
  }

  @Patch('updateMarkerItemById/:id')
  updateMarkerItemById(
    @Body() updateMarkerItem: updateMarker,
    @Param('id') id: string,
  ) {
    return this.markerService.updateMarkerItemById(id, updateMarkerItem);
  }

  @Delete('deleteMarkerItemById/:id')
  deleteMarkerItemById(@Param('id') id: string) {
    return this.markerService.remove(id);
  }
}
